package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.JobregisterForm;
import com.vcaresolution.logistic.pojo.User;
import com.vcaresolution.logistic.to.JobregisterTO;
import com.vcaresolution.logistic.util.ApplicationConstants;
//import org.springframework.validation.FieldError;

/**
 * <code>JobRegisterController</code> is used to track all login related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class JobregisterController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.JOBREGISTERFORM_MODELATTRIBUTE)
	public JobregisterForm createForm(){
		return new JobregisterForm();
	}
	
	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){
		//modelAndView.addObject(ApplicationConstants.VESSELLIST_MODELATTRIBUTE, getServiceRegistry().getVesselService().findByAll());
		modelAndView.addObject(ApplicationConstants.VOYAGELIST_MODELATTRIBUTE, getServiceRegistry().getVoyageService().findVoyageWithVessel());
		modelAndView.addObject(ApplicationConstants.PORTLIST_MODELATTRIBUTE, getServiceRegistry().getPortService().findByAll());
		modelAndView.addObject(ApplicationConstants.CONTACTLIST_MODELATTRIBUTE, getServiceRegistry().getContactService().findByAll());
		modelAndView.addObject(ApplicationConstants.USERLIST_MODELATTRIBUTE,getServiceRegistry().getUserService().findByAll());
		
		modelAndView.addObject(ApplicationConstants.MODELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("MODE"));
		modelAndView.addObject(ApplicationConstants.CATEGORYLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("JRCATEGORY"));
		modelAndView.addObject(ApplicationConstants.CARGOTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CARGOTYPE"));
		modelAndView.addObject(ApplicationConstants.STUFFBYLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("STUFFBY"));		
		modelAndView.addObject(ApplicationConstants.ADDITIONALREQUIREMENTSLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("ADDITIONALREQUIRMENTS"));
		modelAndView.addObject(ApplicationConstants.HAZCLASSLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("HAZCLASS"));
		modelAndView.addObject(ApplicationConstants.HAZPKGGROUPLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("PKGGROUP"));
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDJobRegister Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_JOBREGISTER_URL,method = RequestMethod.GET)
	public ModelAndView loadAddJobRegister() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_JOBREGISTER_VIEW);
		modelAndView.addObject(ApplicationConstants.JOBREGISTERFORM_MODELATTRIBUTE, new JobregisterForm());
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}

	/**
	 * This method is called when the GET request has been made to the ADDJobRegister Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_ADDITIONAL_REQ_URL,method = RequestMethod.GET)
	public ModelAndView loadAddAdditionalRequirement() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_ADDITIONAL_REQ_VIEW);
		
		return modelAndView;
	}
	
	
	/**
	 * This method is called when the POST request has been made to the ADDJobRegister Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_JOBREGISTER_URL,method = RequestMethod.POST)
	public ModelAndView processAddJobRegister(@Valid JobregisterForm jobRegisterForm, 
			 final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());

		//System.out.println("getUnnohumodity : -"+jobRegisterForm.getUnnohumodity());
		
		if( !result.hasErrors() ){
			User loggedInUser = getLoggedInUser(request);
			
			jobRegisterForm.setUser(loggedInUser);
			
			//Setting Job NO
			String branchCode = getLoggedInUserProfileTO(request).getBranchCode();
			jobRegisterForm.setJobno(branchCode+"-"+ApplicationConstants.CODETABLELVALUES.get(jobRegisterForm.getModeid()));
			//END
			
			getServiceRegistry().getJobRegisterService().add(jobRegisterForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_JOBREGISTER_URL, true, true, false));
		} else {
			/*List<FieldError> fieldErrors = result.getFieldErrors();
			System.out.println("===========================================");
			for(FieldError fieldError : fieldErrors){
				System.out.println(" Error "+fieldError.getField()+" Message "+fieldError.getDefaultMessage());
			}
			System.out.println("===========================================");
			
			List<String> fieldErrorMessages = getFieldErrorMessages(result);
			for(String msg:fieldErrorMessages){
				System.out.println("Error message :: "+msg
						);
			}
			*/
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListJobRegister Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_JOBREGISTER_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<JobregisterTO> jobregisterList = getServiceRegistry().getJobRegisterService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.JOBREGISTERLIST_MODELATTRIBUTE, jobregisterList);
		modelAndView.setViewName(ApplicationConstants.LIST_JOBREGISTER_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDJobRegister Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_JOBREGISTER_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditJobRegister(@RequestParam(value="jobnoId") String jobnoId) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object jobregisterObject = getServiceRegistry().getJobRegisterService().findById(Integer.parseInt(jobnoId));
		loadRequiredDetailInModelObject(modelAndView);
		
		modelAndView.addObject(ApplicationConstants.JOBREGISTERFORM_MODELATTRIBUTE, jobregisterObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_JOBREGISTER_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDJobRegister Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_JOBREGISTER_URL,method = RequestMethod.POST)
	public ModelAndView processEditJobRegister(@Valid JobregisterForm jobRegisterForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			
			jobRegisterForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getJobRegisterService().update(jobRegisterForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_JOBREGISTER_URL, true, true, false));
		} else {
			
			loadRequiredDetailInModelObject(modelAndView);	
		}		
		return modelAndView;
	}
}